<?php

namespace app\api\controller;

use app\common\exception\BusinessException;
use think\response\Json;

/**
 * 互助任务
 */
class HelpTask extends Base
{
    /**
     * create
     * @throws BusinessException
     */
    public function create(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'data_id' => 'require',
            'data_type' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'data_id.require' => '数据id不得为空',
            'data_type.require' => '数据type不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        // 提交数据
        $groupId = (new \app\api\logic\HelpTask)->create($param);

        // 返回
        return $this->response(['id' => $groupId]);
    }

    /**
     * save
     * @return Json
     * @throws BusinessException
     */
    public function save(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'id' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'id.require' => '数据id不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        // 提交数据
        (new \app\api\logic\HelpTask)->save($param);

        // 返回
        return $this->response();
    }

    /**
     * itemSave
     * @return Json
     * @throws BusinessException
     */
    public function itemSave(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'id' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'id.require' => '数据id不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        // 提交数据
        (new \app\api\logic\HelpTask)->itemSave($param);

        // 返回
        return $this->response();
    }

    /**
     * info
     * @throws BusinessException
     */
    public function info(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'id' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'id.require' => '数据id不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        // 提交数据
        $info = (new \app\api\logic\HelpTask)->info($param);

        // 返回
        return $this->response($info);
    }

    /**
     * grab
     * @return Json
     * @throws BusinessException
     */
    public function grab(): Json
    {
        // 请求参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'id' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'id.require' => '数据id不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        // 提交数据
        (new \app\api\logic\HelpTask())->grab($param);

        // 返回
        return $this->response();
    }
}
